package com.monkey.tensquare.recruit.service.impl;

import cn.hutool.core.util.StrUtil;
import com.monkey.tensquare.recruit.entity.Enterprise;
import com.monkey.tensquare.recruit.mapper.EnterpriseMapper;
import com.monkey.tensquare.recruit.service.EnterpriseService;
import entity.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class EnterpriseServiceImpl implements EnterpriseService {
    @Autowired
    private EnterpriseMapper enterpriseMapper;
    @Override
    public void add(Enterprise enterprise) {
        enterpriseMapper.save(enterprise);
    }

    @Override
    public List<Enterprise> findAll() {
        return enterpriseMapper.findAll();
    }

    @Override
    public Enterprise findById(String enterpriseId) {
        return enterpriseMapper.findById(enterpriseId).get();
    }

    @Override
    public void update(String enterpriseId, Enterprise enterprise) {
        enterprise.setId(enterpriseId);
        enterpriseMapper.save(enterprise);
    }

    @Override
    public void delete(String enterpriseId) {
        enterpriseMapper.deleteById(enterpriseId);
    }

    @Override
    public List<Enterprise> search(Enterprise enterprise) {
        Specification<Enterprise> specification = createSpecification(enterprise);
        return enterpriseMapper.findAll(specification);
    }

    @Override
    public entity.PageResult<Enterprise> search(Enterprise enterprise, Integer page, Integer size) {
        Specification<Enterprise> specification = createSpecification(enterprise);
        Page<Enterprise> enterprisePage = enterpriseMapper.findAll(specification, PageRequest.of(page - 1, size));
        return new PageResult<Enterprise>(enterprisePage.getTotalElements(),enterprisePage.getContent());
    }

    @Override
    public List<Enterprise> hotlist() {
        Enterprise enterprise = new Enterprise();
        enterprise.setIshot("1");
        Specification<Enterprise> specification = createSpecification(enterprise);
        return enterpriseMapper.findAll(specification);
    }

    private Specification<Enterprise> createSpecification(Enterprise enterprise) {
        return new Specification<Enterprise>() {
            @Override
            public Predicate toPredicate(Root<Enterprise> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (enterprise != null){
                    if (StrUtil.isNotBlank(enterprise.getName())) {
                        predicateList.add(criteriaBuilder.like(root.get("name").as(String.class), "%" + enterprise.getName() + "%"));
                    }
                    if (StrUtil.isNotBlank(enterprise.getSummary())) {
                        predicateList.add(criteriaBuilder.like(root.get("summary").as(String.class), "%" + enterprise.getSummary() + "%"));
                    }
                    if (StrUtil.isNotBlank(enterprise.getAddress())) {
                        predicateList.add(criteriaBuilder.like(root.get("address").as(String.class), "%" + enterprise.getAddress() + "%"));
                    }
                    if (StrUtil.isNotBlank(enterprise.getLabels())) {
                        predicateList.add(criteriaBuilder.like(root.get("labels").as(String.class), "%" + enterprise.getLabels() + "%"));
                    }
                    if (StrUtil.isNotBlank(enterprise.getCoordinate())) {
                        predicateList.add(criteriaBuilder.equal(root.get("coordinate").as(String.class),enterprise.getCoordinate()));
                    }
                    if (StrUtil.isNotBlank(enterprise.getIshot())) {
                        predicateList.add(criteriaBuilder.equal(root.get("ishot").as(String.class), enterprise.getIshot()));
                    }
                    if (StrUtil.isNotBlank(enterprise.getLogo())) {
                        predicateList.add(criteriaBuilder.like(root.get("logo").as(String.class), "%" + enterprise.getLogo() + "%"));
                    }
                    if (StrUtil.isNotBlank(enterprise.getJobcount())) {
                        predicateList.add(criteriaBuilder.equal(root.get("jobcount").as(String.class), enterprise.getJobcount()));
                    }
                    if (StrUtil.isNotBlank(enterprise.getUrl())) {
                        predicateList.add(criteriaBuilder.like(root.get("url").as(String.class), "%" + enterprise.getUrl() + "%"));
                    }
                }
                Predicate[] predicates = new Predicate[predicateList.size()];
                predicates = predicateList.toArray(predicates);
                return criteriaBuilder.and(predicates);
            }
        };
    }
}
